package com.samehope.ar.controller;


import com.samehope.ar.common.CommonResult;
import com.samehope.ar.dto.ProblemTypeAddParam;
import com.samehope.ar.dto.ProblemTypeUpdateParam;
import com.samehope.ar.service.IProblemTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 问题分类表 前端控制器
 * </p>
 *
 * @author Zhang Luo
 * @since 2020-01-06
 */
@RestController
@RequestMapping("/problemType")
public class ProblemTypeController {

    @Autowired
    private IProblemTypeService problemTypeService;

    /**
     * 新增问题分类
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PostMapping("/add")
    public CommonResult add(@Valid @RequestBody ProblemTypeAddParam param) {
        problemTypeService.add(param);
        return CommonResult.success(null);
    }

    /**
     * 更新问题分类
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PutMapping("/update")
    public CommonResult update(@Valid @RequestBody ProblemTypeUpdateParam param) {
        problemTypeService.update(param);
        return CommonResult.success(null);
    }

    /**
     * 删除问题分类
     * @param problemTypeId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @DeleteMapping("/delete/{problemTypeId}")
    public CommonResult delete(@PathVariable("problemTypeId") Long problemTypeId) {
        problemTypeService.deleteProblemType(problemTypeId);
        return CommonResult.success(null);
    }

    /**
     * 问题分类下拉选的视图对象结合
     * @return
     */
    @GetMapping("/typesForSelect")
    public CommonResult typesForSelect() {
        return CommonResult.success(problemTypeService.typesForSelect());
    }
}
